package com.pig4cloud.pig.module.service.mer.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.entity.SysUser;
import com.pig4cloud.pig.common.core.exception.TransactionalException;
import com.pig4cloud.pig.common.core.util.MathUtil;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.module.api.entity.mer.MerSelfVip;
import com.pig4cloud.pig.module.api.entity.mer.MerSelfVipRechargeRecord;
import com.pig4cloud.pig.module.mapper.mer.MerSelfVipMapper;
import com.pig4cloud.pig.module.mapper.mer.MerSelfVipRechargeRecordMapper;
import com.pig4cloud.pig.module.service.mer.IMerSelfVipService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 商家的自有会员Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-28
 */
@Service
public class MerSelfVipServiceImpl extends ServiceImpl<MerSelfVipMapper, MerSelfVip> implements IMerSelfVipService {

    @Resource
    private MerSelfVipMapper selfVipMapper;
    @Resource
    private MerSelfVipRechargeRecordMapper selfVipRechargeRecordMapper;

    @Override
    @Transactional
    public int recharge(MerSelfVip merSelfVip, PigUser pigUser) {
        int ret = 0;
        try{

            MerSelfVipRechargeRecord rechargeRecord = new MerSelfVipRechargeRecord();
            rechargeRecord.setSelfVipId(merSelfVip.getId());
            rechargeRecord.setBeforeMoney(merSelfVip.getAccountMoney());
            if(merSelfVip.getRechargeMoney()!= null && merSelfVip.getRechargeMoney().doubleValue() > 0){
                if(merSelfVip.getAccountMoney() == null){
                    merSelfVip.setAccountMoney(merSelfVip.getRechargeMoney());
                }else{
                    double accountMoney = merSelfVip.getAccountMoney().doubleValue();
                    accountMoney += merSelfVip.getRechargeMoney().doubleValue();

                    double rechargeTotal = merSelfVip.getRechargeTotalMoney().doubleValue();
                    rechargeTotal += merSelfVip.getRechargeMoney().doubleValue();

                    merSelfVip.setRechargeTotalMoney(new BigDecimal(MathUtil.getDoubleUp(rechargeTotal)));
                    merSelfVip.setAccountMoney(new BigDecimal(MathUtil.getDoubleUp(accountMoney)));
                }
            }

            ret = selfVipMapper.updateById(merSelfVip);
            if(ret <= 0){
                throw new TransactionalException("selfVipMapper updateById error");
            }

            rechargeRecord.setMerId(merSelfVip.getMerId());
            rechargeRecord.setRechargeMoney(merSelfVip.getRechargeMoney());
            rechargeRecord.setVipName(merSelfVip.getRealName());
            rechargeRecord.setVipPhone(merSelfVip.getPhoneNum());
            rechargeRecord.setRechargeType(1);
            rechargeRecord.setOpSysUserId(pigUser.getId());
            rechargeRecord.setOpSysUserName(pigUser.getName());
            rechargeRecord.setCreateTime(new Date());
            rechargeRecord.setRemark("会员充值费用");
            ret = selfVipRechargeRecordMapper.insert(rechargeRecord);
            if(ret <= 0){
                throw new TransactionalException("selfVipRechargeRecordMapper insert error");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("recharge updateById error");
        }
        return ret;
    }
    @Override
    @Transactional
    public int charge(MerSelfVip merSelfVip, PigUser sysUser) {
        int ret = 0;
        try{

            MerSelfVipRechargeRecord rechargeRecord = new MerSelfVipRechargeRecord();
            rechargeRecord.setSelfVipId(merSelfVip.getId());
            rechargeRecord.setBeforeMoney(merSelfVip.getAccountMoney());
            if(merSelfVip.getRechargeMoney()!= null && merSelfVip.getRechargeMoney().doubleValue() > 0){
                if(merSelfVip.getAccountMoney() == null){
                    merSelfVip.setAccountMoney(merSelfVip.getRechargeMoney());
                }else{
                    double accountMoney = merSelfVip.getAccountMoney().doubleValue();
                    accountMoney -= merSelfVip.getRechargeMoney().doubleValue();

                    merSelfVip.setRechargeTotalMoney(new BigDecimal(MathUtil.getDoubleUp(merSelfVip.getMoney())));
                    merSelfVip.setAccountMoney(new BigDecimal(MathUtil.getDoubleUp(accountMoney)));
                }
            }

            ret = selfVipMapper.updateById(merSelfVip);
            if(ret <= 0){
                throw new TransactionalException("selfVipMapper updateById error");
            }

            rechargeRecord.setMerId(merSelfVip.getMerId());
            rechargeRecord.setRechargeMoney(BigDecimal.valueOf(merSelfVip.getMoney()));
            rechargeRecord.setVipName(merSelfVip.getRealName());
            rechargeRecord.setVipPhone(merSelfVip.getPhoneNum());
            rechargeRecord.setRechargeType(2);
            rechargeRecord.setOpSysUserId(sysUser.getId());
            rechargeRecord.setOpSysUserName(sysUser.getName());
            rechargeRecord.setCreateTime(new Date());
            rechargeRecord.setRemark("独一处消费");
            ret = selfVipRechargeRecordMapper.insert(rechargeRecord);
            if(ret <= 0){
                throw new TransactionalException("selfVipRechargeRecordMapper insert error");
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("recharge updateById error");
        }
        return ret;
    }
}



